﻿@model Model.AppUserList
@{
    ViewBag.Title = "商品信息维护";     //望湘园系统使用的"商品信息维护"
    Layout = "~/Views/Shared/_JQBaseLayOut.cshtml";
}
@using Model

@section JQscriptSection{
    <script type="text/javascript">
    var _getListUrl = '@Url.Action("InitJsonResult", "GoodsInfo")';
    var _saveDataUrl = '@Url.Action("SaveData", "GoodsInfo")';
    var _getInventoryUrl = '@Url.Action("GetInventory", "GoodsInfo")';
    $(document).ready(function ()
    {

        $.initKeyDown("btnSerach");
        InitGrid();
        initInputValidate();
        //$('#STATUS').combobox({
        //  //  editable: false,
        //    onChange: function (newValue, oldValue)
        //    {
        //       // $(this).combobox('setValue', oldValue);        //恢复成老的选择
        //       // if ($("#hidSTATUS").val() != newValue && $("#hidIsChange").val()=="false")  //改动，但是库存非0
        //        {
        //           // alert("库存必须为0，状态不能修改");                   
        //           // layer.msg("库存必须为0，状态不能修改", 1);
        //          //  $('#STATUS').combobox('setValue', oldValue);        //恢复成老的选择
        //           // $('#STATUS').combobox('setText', "有效");        //恢复成老的选择
        //        }
        //        checkStatus(newValue);
        //      //  $("#hidSTATUS").val(newValue);
        //      //  $('#STATUS').combobox('setValue', newValue);
        //      //  $('#STATUS').validatebox('validate');
        //    }
        //});
        HandleDom.ClassTriggerClick("icon-add", function ()
        {
            HandleDialog.OpenDialogByAdd('dlg', '添加', function ()
            {
                $("#formEdit")[0].reset();
                $('#GOODSID').val(0);
                //  $('#STATUS').combobox('setValue', 1);
                $('#STATUS').val(1);
                $('#hidIsChange').val("true");
                changeGoodsCode();
                initGoodsCodeValidate();
            });
        });

        HandleDom.ClassTriggerClick("icon-reload", function ()
        {
            InitGrid();
        });

        HandleDom.ClassTriggerClick("icon-edit", function ()
        {
            var row = HandleData.GetDataGridSelected("dg");
            edit(row);
        });
    });

    
    function initInputValidate()
    {
        initGoodsCodeValidate();

        $('#GOODSNAME').validatebox({
            required: true,
        });

        $('#GOODSSPEC').validatebox({
            required: true,
            //  validType: 'minLength[3]'
        });
        $('#UNITNAME').validatebox({
            required: true,
            validType: "isChanged['UNITNAME','hidIsChange','库存必须为0']",
        });
        $('#PURUNITNAME').validatebox({
            required: true,
        });
        $('#PURUNITQTY').validatebox({
            required: true,
            validType: ["floatOrInt", "isChanged['PURUNITQTY','hidIsChange','库存必须为0']",
                        "isCompared['值必须为1','UNITNAME','PURUNITNAME',1]"]
        });
       
        $('#STATUS').validatebox({
           // required: true,
            validType: "isChanged['STATUS','hidIsChange','库存必须为0']",
        });
        
    }
    function checkStatus(value)
    {
        if (!value)
            value = $('#STATUS').combobox("getValue");
        var isValid = $("#hidSTATUS").val() != value && $("#hidIsChange").val() == "false";
        if (isValid)  //改动，但是库存非0
        {
            // alert("库存必须为0，状态不能修改");                   
            layer.msg("库存必须为0，状态不能修改", 1);
        }
        return !isValid;
    }
    
    function initGoodsCodeValidate()
    {
        if ($('#GOODSID').val() == 0)
            $('#GOODSCODE').validatebox({
                required: true,
                invalidMessage: "商品编号已存在",
                validType: "remote['@Url.Action("ExistsGoodsCode", "GoodsInfo")', 'GOODSCODE']"
            });
        else
            $('#GOODSCODE').validatebox({ required: false, validType: "" });
    }
    //编辑
    function edit(data)
    {
        if (!data)
        {
            HandleTips.Alert("请选中数据");
            return;
        }
        HandleDialog.OpenDialog(data, 'dlg', '编辑', function ()
        {
            $('#GOODSID').val(data.GOODSID);
            $("#GOODSCODE").val(data.GOODSCODE);
            $("#GOODSNAME").val(data.GOODSNAME);
            $("#GOODSSPEC").val(data.GOODSSPEC);
            $("#UNITNAME").val(data.UNITNAME);
            $("#UNITNAME").attr("value_old", data.UNITNAME);
            $("#PURUNITNAME").val(data.PURUNITNAME);
            $("#PURUNITQTY").val(data.PURUNITQTY);
            $("#PURUNITQTY").attr("value_old", data.PURUNITQTY);
          //  $("#hidSTATUS").val(data.STATUS);
           // $('#STATUS').combobox('setValue', data.STATUS);
            $("#STATUS").val(data.STATUS);
            $("#STATUS").attr("value_old", data.STATUS);

            $("#RULECLASS").val(data.RULECLASS);
            changeGoodsCode();
            initGoodsCodeValidate();

            $.ajax({
                type: "post",         //请求类型,get post
                async: false,
                url: '@Url.Action("GetInventory", "GoodsInfo")',
                data: { GOODSID: $("#GOODSID").val() },             //传入参数
                // dataType: "json",    //返回类型
                success: function (response)
                {
                    $("#hidIsChange").val(response == 0);
                }
            });
        });
    }


    function saveData()
    {
        HandleRequest.submitForm('formEdit', _saveDataUrl, function (data)
        {
            $.log('data=' + data);
            data = $.parseJSON(data);
            if (data.IsSuccess)
            {
                layer.alert('执行成功!', 9);
                InitGrid();
                HandleDialog.CloseDialog('dlg');
            }
            else
            {
                changeGoodsCode();
                layer.msg(data.Message, 1);
            }
        },
        function (data)   //提交前
        {
            $("#GOODSCODE").removeAttr("disabled");
            var isValid = $("#formEdit").form('validate');
            if(!isValid)
                changeGoodsCode();

            return isValid;
            //alert(1);
        });
    }

    function changeGoodsCode(isDisabled)
    {
        if ($('#GOODSID').val() > 0)
            $("#GOODSCODE").attr("disabled", "disabled");
        else
            $("#GOODSCODE").removeAttr("disabled");
    }

    function InitGrid()
    {
        var queryParams = $.formToJson("query");
        $.log(queryParams);
        var otherParamm = { toolbar: "toolbar", idField: "GOODSID" };
        //表格的列设置
        var columns = [[
               { field: 'GOODSCODE', title: '商品编码', width: 30, align: 'left' },
               { field: 'GOODSNAME', title: '商品名称', width: 80, },
               { field: 'GOODSSPEC', title: '商品规格', width: 50, },
               { field: 'UNITNAME', title: '库存单位', width: 30, },
               { field: 'PURUNITNAME', title: '采购单位', width: 30, },
               { field: 'PURUNITQTY', title: '采购换算率', width: 20, },
               { field: 'STATUSText', title: '状态', width: 20, },
               { field: 'ISPURUNITNAMEText', title: '单位是否一致', width: 20, },
               { field: 'RULECLASS', title: '可售期规则类别', width: 50, },
        ]];
        HandleData.InitDataGridPage('dg', _getListUrl, queryParams,
            columns,
            otherParamm,
            function (data)
            {
            },
            function (rowIndex, rowData)
            {//双击
                edit(rowData);
            });
    }
    </script>
}

<body style="background-color: ButtonFace; font-size: 17px">
    <!--查询条件-->
    <div class="easyui-panel" title="查询条件" style="height: 100px; padding: 5px; background: #fafafa; width: 1500px;">
        <form id="query" method="post">
            <table>
                <tr>
                    <td>
                        商品编号:
                    </td>
                    <td>
                        <input type="hidden" id="selectList" name="selectList" />
                        <input id="txtGOODSCODE" name="GOODSCODE" style="width: 154px" />
                    </td>
                    <td>
                        商品名称:
                    </td>
                    <td>
                        <input id="txtGOODSNAME" name="GOODSNAME" style="width: 154px" />
                    </td>
                    <td>可售期规则类别:</td>
                    <td>
                        <input id="txtRULECLASS" name="RULECLASS" style="width: 100px;" />
                    </td>
                    <td>
                        状态:
                    </td>
                    <td>
                        <select id="ddlSTATUS" class="easyui-combobox" name="STATUS" style="width: 100px;">
                            <option value="">全部</option>
                            <option value="1">有效</option>
                            <option value="9">无效</option>
                        </select>
                    </td>
                    <td>
                        <a id="btnSerach" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="InitGrid();">查询</a>
                    </td>
                </tr>
            </table>
        </form>
    </div>
    <!--数据列表-->
    <div style="min-height: 720px; width: 1500px;">
        <table id="dg" title="商品信息" style="min-height: 600px;"></table>
    </div>
    <!--添加和编辑弹出框-->
    <div id="dlg" class="easyui-dialog" style="width: 620px; height: 200px; top: 300px; left: 400px; padding: 2px"
         data-options="closed:true,buttons:'#dlg-buttons',modal:true">
        <form id="formEdit" method="post">
            <input id="GOODSID" name="GOODSID" type="hidden" />
            <input id="hidIsChange" name="hidIsChange" type="hidden" />
            <table>
                <tr>
                    <td>
                        商品编码:
                    </td>
                    <td>
                        <input id="GOODSCODE" name="GOODSCODE" maxlength="30" style="width: 154px;" />
                    </td>
                    <td>
                        商品名称:
                    </td>
                    <td colspan="2">
                        <input id="GOODSNAME" name="GOODSNAME" style="width: 204px" />
                    </td>
                </tr>
                <tr>
                    <td>
                        商品规格:
                    </td>
                    <td>
                        <input id="GOODSSPEC" name="GOODSSPEC" style="width: 155px;">
                    </td>
                    <td>
                        库存单位:
                    </td>
                    <td colspan="2">
                        <input id="UNITNAME" name="UNITNAME" style="width: 204px;">
                    </td>
                </tr>
                <tr>
                    <td>
                        采购单位:
                    </td>
                    <td>
                        <input id="PURUNITNAME" name="PURUNITNAME" class="easyui-validatebox" style="width: 154px" />
                    </td>
                    <td>
                        采购换算率:
                    </td>
                    <td>
                        <input id="PURUNITQTY" name="PURUNITQTY" class="easyui-validatebox" style="width: 204px" />
                    </td>
                </tr>
                <tr>
                    <td>
                        状态:
                    </td>
                    <td>
                        <select id="STATUS" name="STATUS" style="width: 154px;">
                            <option value="1">有效</option>
                            <option value="9">无效</option>
                        </select>
                        <input id="hidSTATUS" name="hidSTATUS" type="hidden"/>
                    </td>
                    <td>可售期规则类别</td>
                    <td>
                        <input id="RULECLASS" name="RULECLASS" style="width: 204px" />
                    </td>
                    <td colspan="2"></td>

                </tr>
                <tr>
                    <td colspan="2"></td>
                    <td>
                        <a id="btnSave" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="saveData()">保存</a>
                    </td>
                </tr>

            </table>
        </form>
    </div>
    <!--数据列表上的工具条-->
    @Html.Partial("~/Views/Shared/_PartialToolbar.cshtml", new Toolbar
{
    GridName = "dg",
    GridType = "datagrid",
    ToolbarName = "toolbar",
    FunctionModule = new Dictionary<string, List<FunctionModule>>()
    {
        {"Function",new List<FunctionModule>()
            {
              new FunctionModule(){Name = "Add",IsEnable =true },
              new FunctionModule(){Name = "Edit",IsEnable =true },
              new FunctionModule(){Name = "Remove",IsEnable =false },
              new FunctionModule(){Name = "Reload",IsEnable =false },
              new FunctionModule(){Name = "Collapse",IsEnable =false },
              new FunctionModule(){Name = "Exporter",IsEnable =false },
              new FunctionModule(){Name = "AllEdit",IsEnable =false },


            }
        }
    }
})

</body>


